package com.programmercarl.stackandqueue;

import java.util.ArrayDeque;
import java.util.Deque;

/**
 * https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/
 *
 * @author yulongtian
 * @create 2024-12-16 10:07
 */
public class Test01 {
    public static void main(String[] args) {
        Test01 test01 = new Test01();
        System.out.println(test01.removeDuplicates("abbaca"));
    }

    public String removeDuplicates(String s) {
        Deque<Character> stack = new ArrayDeque<Character>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!stack.isEmpty() && stack.peek() == c) {
                stack.pop();
            } else {
                stack.push(c);
            }
        }
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }

        return sb.reverse().toString();
    }

}
